* #####################################
* # Replication Material for 
* # Stefan Müller and Michael Jankowski:
* # Do voters really prefer more choice? Determinants of support for personalised electoral systems.
* # Journal of Elections, Public Opinion and Parties.
* #####################################

* Note: This script runs all regression models and saves the predicted 
* probabilities in the folder "data_effects". Afterwards, run the file "03_create_plots.R".
* To run this file, Stata 13 (or higher) needs to be installed.

* Set directories (example – users will need to adjust this based on their working directory)
cd "/Users/stefan/Dropbox/wahlsystem-hh-hb/mueller_jankowski_jepop_2018"
use "/Users/stefan/Dropbox/wahlsystem-hh-hb/mueller_jankowski_jepop_2018/hh_hb_11_15_prepared.dta", clear

set more off 

* Election ID

egen election_id = group(election), label

* Rename Dependent Variable

gen dep = ""
replace dep = "Like" if pref_law == 1
replace dep = "Dislike" if pref_law == 2
replace dep = "Indifferent" if pref_law == 3
egen dv = group(dep), label

* Voter Types ('Splitting')

gen split = ""
replace split = "Nichtwaehler" if splitting == 0
replace split = "Waehler - Kein Splitting" if splitting == 1
replace split = "Waehler - Splitting" if splitting == 2
drop splitting
egen splitting = group(split), label

* Age

egen age = group(age_group), label

* Education (No Abi, Abi, University)

gen abi = .
replace abi = 0 if !missing(education)
replace abi = 1 if education == 3 & university == 2
replace abi = 2 if education == 3 & university == 1
gen edu = ""
replace edu = "Kein Abitur" if abi == 0
replace edu = "Abitur - kein Studium" if abi == 1
replace edu = "Abitur - Studium" if abi == 2
drop abi
egen abi = group(edu), label

* Party ID

gen pid = ""
replace pid = "Keine" if party_id == 10
replace pid = "Andere" if party_id == 8 | party_id == 9
replace pid = "SPD" if party_id == 1
replace pid = "CDU" if party_id == 2 | party_id == 3 | party_id == 4
replace pid = "FDP" if party_id == 5
replace pid = "Gruene" if party_id == 6
replace pid = "Linke" if party_id == 7
egen pident = group(pid), label

* Political Interest

gen pol_int_new = ""
replace pol_int_new = "Hoch" if pol_int < 3
replace pol_int_new = "Mittel" if pol_int == 3
replace pol_int_new = "Niedrig" if pol_int > 3
drop pol_int
egen pol_int = group(pol_int_new), label

* Satisfaction with Party ID

gen satisfaction = .
replace satisfaction = rate_cdu_bund if pid == "CDU"
replace satisfaction = rate_spd_bund if pid == "SPD"
replace satisfaction = rate_green_bund if pid == "Gruene"
replace satisfaction = rate_left_bund if pid == "Linke"
replace satisfaction = rate_fdp_bund if pid == "FDP"
egen mean_rate_bund = rowmean(rate_cdu_bund rate_spd_bund rate_green_bund rate_left_bund rate_fdp_bund)
egen max_rate_bund = rowmax(rate_cdu_bund rate_spd_bund rate_green_bund rate_left_bund rate_fdp_bund)
replace satisfaction = max_rate_bund if satisfaction == .

* Winner Last Election

egen win = group(winner), label

***************************************************************************************************************************************************************************************
***************************************************************************************************************************************************************************************
***************************************************************************************************************************************************************************************
***************************************************************************************************************************************************************************************
* REGRESSION MODELS *******************************************************************************************************************************************************************
***************************************************************************************************************************************************************************************
***************************************************************************************************************************************************************************************
***************************************************************************************************************************************************************************************
***************************************************************************************************************************************************************************************

* (1) MAIN MODEL FOR PLOTS AND SUBMISSION

mlogit dv ib2.pident i.abi i.age i.election_id i.pol_int i.gender i.splitting c.satisfaction i.win, r baseoutcome(1)
est store reg_mod // Store Results
*esttab using reg_table_hh_hb.rtf, replace c(b(star fmt(%9.3f)) se(par fmt(%9.3f))) sty(fixed) s(ll chi2 N) label legend varlabel(_cons Constant) unstack nobaselevel noomitted

* (2) WITHOUT VOTER TYPE (POTENTIALLY ENDOGENOUS)

mlogit dv ib2.pident i.abi i.age i.election_id i.pol_int i.gender c.satisfaction i.win, r baseoutcome(1)
est store reg_mod_2 // Store Results

* (3) ALL ELECTIONS SEPERATELY

mlogit dv ib2.pident i.abi i.age i.pol_int i.gender i.splitting c.satisfaction i.win if election_id == 1, r baseoutcome(1)
est store reg_mod_hb_2011 

mlogit dv ib2.pident i.abi i.age i.pol_int i.gender i.splitting c.satisfaction i.win if election_id == 2, r baseoutcome(1)
est store reg_mod_hb_2015 

mlogit dv ib2.pident i.abi i.age i.pol_int i.gender i.splitting c.satisfaction i.win if election_id == 3, r baseoutcome(1)
est store reg_mod_hh_2011 

mlogit dv ib2.pident i.abi i.age i.pol_int i.gender i.splitting c.satisfaction i.win if election_id == 4, r baseoutcome(1)
est store reg_mod_hh_2015 

* (4) EXPORT FULL REG TABLE

esttab reg_mod reg_mod_2 reg_mod_hb_2011 reg_mod_hb_2015 reg_mod_hh_2011 reg_mod_hh_2015 using reg_table_hh_hb_RR.rtf, replace c(b(star fmt(%9.3f)) se(par fmt(%9.3f))) sty(fixed) s(ll chi2 N) label legend varlabel(_cons Constant) unstack nobaselevel noomitted

*************************************************************
* Estimate Predicted Probabilities (using Stata 13 approach)
*************************************************************

forval i = 1/3{
est restore reg_mod
margins, at(satisfaction = (0(1)10)) predict(outcome(`i')) post
mexport, file("satisfaction_`i'")
}
*

forval i = 1/3{
est restore reg_mod
margins pident, predict(outcome(`i')) post
mexport, file("pident_`i'")
}
*

forval i = 1/3{
est restore reg_mod
margins pol_int, predict(outcome(`i')) post
mexport, file("pol_int_`i'")
}
*

forval i = 1/3{
est restore reg_mod
margins abi, predict(outcome(`i')) post
mexport, file("abi_`i'")
}
*

forval i = 1/3{
est restore reg_mod
margins age, predict(outcome(`i')) post
mexport, file("age_`i'")
}
*

forval i = 1/3{
est restore reg_mod
margins splitting, predict(outcome(`i')) post
mexport, file("splitting_`i'")
}
*

forval i = 1/3{
est restore reg_mod
margins win, predict(outcome(`i')) post
mexport, file("win_`i'")
}
*

*******************************************************************
* Robustness Checks
*******************************************************************

* Pol. knowledge instead of political interest

mlogit dv i.pident i.abi i.age i.election_id ib2.pol_knowledge_dummy i.gender i.splitting c.satisfaction i.win, r baseoutcome(1)
est store reg_mod_know // Store Results

forval i = 1/3{
est restore reg_mod_know
margins pol_knowledge_dummy, predict(outcome(`i')) post
mexport, file("pol_know_`i'")
}
*

* Local Party ID

egen pident_local = group(party_id_local), label

mlogit dv i.pident_local i.abi i.age i.election_id i.pol_int i.gender i.splitting c.satisfaction i.win, r baseoutcome(1)
est store reg_mod_local // Store Results

forval i = 1/3{
est restore reg_mod_local
margins pident_local, predict(outcome(`i')) post
mexport, file("pident_local_`i'")
}
*

* Age Fine Grained

mlogit dv i.pident i.abi i.age_fine i.election_id i.pol_int i.gender i.splitting c.satisfaction i.win, r baseoutcome(1)
est store reg_mod_age // Store Results

forval i = 1/3{
est restore reg_mod_age
margins age_fine, predict(outcome(`i')) post
mexport, file("age_fine_`i'")
}
*


**************************************
********** PARTY RATINGS
**************************************

	mlogit dv c.rate_green i.abi i.age i.election_id i.pol_int i.gender i.splitting c.satisfaction i.win, r baseoutcome(1)
	est store reg_mod_green
	forval i = 1/3{
	est restore reg_mod_green
	margins, at(rate_green = (0(1)10)) predict(outcome(`i')) post
	mexport, file("rating_green_`i'")
	}
	*

	mlogit dv c.rate_cdu i.abi i.age i.election_id i.pol_int i.gender i.splitting c.satisfaction, r baseoutcome(1)
	est store reg_mod_cdu
	forval i = 1/3{
	est restore reg_mod_cdu
	margins, at(rate_cdu = (0(1)10)) predict(outcome(`i')) post
	mexport, file("rating_cdu_`i'")
	}
	*

	mlogit dv c.rate_fdp i.abi i.age i.election_id i.pol_int i.gender i.splitting c.satisfaction, r baseoutcome(1)
	est store reg_mod_fdp
	forval i = 1/3{
	est restore reg_mod_fdp
	margins, at(rate_fdp = (0(1)10)) predict(outcome(`i')) post
	mexport, file("rating_fdp_`i'")
	}
	*

	mlogit dv c.rate_left i.abi i.age i.election_id i.pol_int i.gender i.splitting c.satisfaction, r baseoutcome(1)
	est store reg_mod_left
	forval i = 1/3{
	est restore reg_mod_left
	margins, at(rate_left = (0(1)10)) predict(outcome(`i')) post
	mexport, file("rating_left_`i'")
	}
	*

	mlogit dv c.rate_spd i.abi i.age i.election_id i.pol_int i.gender i.splitting c.satisfaction, r baseoutcome(1)
	est store reg_mod_spd
	forval i = 1/3{
	est restore reg_mod_spd
	margins, at(rate_spd = (0(1)10)) predict(outcome(`i')) post
	mexport, file("rating_spd_`i'")
	}
	*

***********************************************************************
* Robustness: Predictions for Each Election
***********************************************************************

* Run seperately for each election

local testthat hb_2011 hb_2015 hh_2011 hh_2015

foreach l of local testthat{

mlogit dv ib2.pident i.abi i.age i.pol_int i.gender i.splitting c.satisfaction i.win if election == "`l'", r baseoutcome(1)
est store reg_mod

forval i = 1/3{
est restore reg_mod
margins, at(satisfaction = (0(1)10)) predict(outcome(`i')) post
mexport, file("satisfaction_`l'_`i'")
}
*

forval i = 1/3{
est restore reg_mod
margins pident, predict(outcome(`i')) post
mexport, file("pident_`l'_`i'")
}
*

forval i = 1/3{
est restore reg_mod
margins pol_int, predict(outcome(`i')) post
mexport, file("pol_int_`l'_`i'")
}
*

forval i = 1/3{
est restore reg_mod
margins abi, predict(outcome(`i')) post
mexport, file("abi_`l'_`i'")
}
*

forval i = 1/3{
est restore reg_mod
margins age, predict(outcome(`i')) post
mexport, file("age_`l'_`i'")
}
*

forval i = 1/3{
est restore reg_mod
margins splitting, predict(outcome(`i')) post
mexport, file("splitting_`l'_`i'")
}
*

forval i = 1/3{
est restore reg_mod
margins win, predict(outcome(`i')) post
mexport, file("win_`l'_`i'")
}
*
}
*

***********************************************
* INTERACTION: PI and Satisfaction
***********************************************

gen supporters = pident == 4 | pident == 6

mlogit dv i.supporters##c.satisfaction i.abi i.age i.election_id i.pol_int i.gender i.splitting i.win, r baseoutcome(1)
est store reg_mod
forval i = 1/3{
est restore reg_mod
margins supporters, at(satisfaction = (3(1)10)) predict(outcome(`i')) post
mexport, file("supporters_int_`i'")
}








